//
//  TransactionRecordController.m
//  DianDangDai
//
//  Created by bhf on 2017/12/20.
//  Copyright © 2017年 bhf. All rights reserved.
//

#import "TransactionRecordController.h"
#import "TransactionRecordCell.h"
#import "JYJLModel.h"
@interface TransactionRecordController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *startTime;
@property (strong, nonatomic) IBOutlet UITextField *endTime;
@property (nonatomic, strong) UIDatePicker *DatePicker;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) UIToolbar *toolbar;
@property (nonatomic, assign) NSInteger tag;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, assign) NSInteger currtag;
@end

@implementation TransactionRecordController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"交易记录";
    _currtag = 101;
    _type = 1;
    //设置时间选择器
    [self setPickerUI];
    [self setupTextView];
    [self requestData];
    // Do any additional setup after loading the view.
}

- (void)requestData {
    self.dataArray = [NSMutableArray array];
    NSString *urlString = [NSString stringWithFormat:@"%@OPT=1025&sign=%@&type=%ld&currPage=1&beginTime=%@&endTime=%@",API_LOGIN_URL_STRING,USER_SIGN,_type,_startTime.text,_endTime.text];
    [[CDHttpHelper defaultCDHttpHelper] get:urlString params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
        NSLog(@"%@",dict);
        NSInteger code = [dict[@"code"] integerValue];
        if (code == -1) {
            [self.view makeToast:dict[@"msg"]];
        }
        for (NSDictionary *dic in dict[@"list"]) {
            JYJLModel *model = [JYJLModel mj_objectWithKeyValues:dic];
            [self.dataArray addObject:model];
        }
        //[self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    } showHUD:YES];
}


- (void)setPickerUI{
    
    
    self.DatePicker =[[UIDatePicker alloc]init];
    [self.DatePicker setLocale:[NSLocale localeWithLocaleIdentifier:@"zh-CN"]];
    self.DatePicker.datePickerMode=UIDatePickerModeDate;
    self.DatePicker.minimumDate = [NSDate date];
    //    [self.view addSubview:date];
    [self.DatePicker addTarget:self action:@selector(dateAction:) forControlEvents:UIControlEventValueChanged];
    _toolbar =[[UIToolbar alloc]init];
    //设置工具条的颜色
    _toolbar.barTintColor=[UIColor groupTableViewBackgroundColor];
    //设置工具条的frame
    _toolbar.frame=CGRectMake(0, 0, self.view.frame.size.width, 44);
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(Action:)];
    UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cacelAction:)];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    UIBarButtonItem *bleSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    item2.tag = 100;
    _toolbar.items = @[spaceItem,item,bleSpaceItem,item2,spaceItem];
    self.endTime.placeholder = @"选择结束时间";
    self.endTime.inputView = self.DatePicker;
    self.endTime.inputAccessoryView = self.toolbar;
    self.startTime.placeholder = @"选择开始时间";
    self.startTime.inputView = self.DatePicker;
    self.startTime.inputAccessoryView = self.toolbar;
    _endTime.tag = 101;
    _startTime.tag = 102;
    _endTime.delegate = self;
    _startTime.delegate = self;
   // self.endTime.enabled = NO;
    
}

#pragma textField delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    _tag = textField.tag;
}

#pragma mark - DatePick
- (void)cacelAction:(UIBarButtonItem *)sender{
    if (_tag == 101) {
        [self.endTime resignFirstResponder];
    } else if (_tag == 102) {
        [self.startTime resignFirstResponder];
    }
    
}
- (void)dateAction:(UIDatePicker *)datePicker
{
    // 取出时间
    NSDate *date = datePicker.date;
    // NSDate与NSString之间的转换
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat =@"yyyy.MM.dd";
    NSString *time = [formatter stringFromDate:date];
    self.endTime.text = time;
    // self.time = time;
}
- (void)Action:(UIBarButtonItem *)sender{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat =@"yyyy.MM.dd";
    NSDate *selected = [self.DatePicker date];
    self.date = selected;
    NSString *time1 = [formatter stringFromDate:selected];
    sender.tag  = 100;
    if (_tag == 101) {
        self.endTime.text = time1;
        [self.endTime resignFirstResponder];
    }
    if (_tag == 102) {
        self.startTime.text = time1;
        [self.startTime resignFirstResponder];
    }
    
    
}

- (void)setupTextView{
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"riqi.jpg"]];
    imageView.frame = CGRectMake(0, 5, 20, 20);
    UIView *rightView = [[UIView alloc] init];
    rightView.size = CGSizeMake(30, 30);
    [rightView addSubview:imageView];
    self.startTime.rightView = rightView;
    self.startTime.rightViewMode = UITextFieldViewModeAlways;
    
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"riqi.jpg"]];
    imageView2.frame = CGRectMake(0, 5, 20, 20);
    UIView *rightView2 = [[UIView alloc] init];
    rightView2.size = CGSizeMake(30, 30);
    [rightView2 addSubview:imageView2];
    self.endTime.rightView = rightView2;
    self.endTime.rightViewMode = UITextFieldViewModeAlways;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSString *identfier;
    JYJLModel *model = self.dataArray[indexPath.row];
    if ([model.type isEqualToString:@"收入"]) {
        identfier = @"transactionRecord_cellI";
    } else {
        identfier = @"transactionRecord_cellII";
    }
    
    TransactionRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier forIndexPath:indexPath];
    [cell setModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 130;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)changeData:(UIButton *)sender {
    
    for (UIButton *btn in _topView.subviews) {
        [btn setTitleColor:[UIColor colorWithHex:@"#333333"] forState:UIControlStateNormal];
    }
    [sender setTitleColor:THE_MASS_TONE_ATTUNE forState:UIControlStateNormal];
    switch (sender.tag) {
        case 101:
            _type = 1;
            break;
        case 102:
            _type = 2;
            break;
        case 103:
            _type = 3;
            break;
        case 104:
            _type = 4;
            break;
        case 105:
            _type = 5;
            break;
        default:
            break;
    }
    if (_currtag != sender.tag) {
        [self requestData];
    }
    _currtag = sender.tag;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self requestData];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
